//
//  ShellCoinDetailHeadView.m
//  ShellWallert
//
//  Created by lvzhao on 2020/5/10.
//  Copyright © 2020 VV. All rights reserved.
//

#import "ShellCoinDetailHeadView.h"
#import "ShellCoinDetailViewModel.h"
#import "OYCountDownManager.h"

@interface ShellCoinDetailHeadView()

@property (nonatomic,strong) ShellCoinDetailViewModel *viewModel;

@property (nonatomic,strong) UILabel *timeLab;


@end
@implementation ShellCoinDetailHeadView

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
        self.viewModel = (ShellCoinDetailViewModel *)viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    
    UIView *contentView = [[UIView alloc] init];
    contentView.cornerRadius = 10;
    contentView.backgroundColor = UIColorHex(0x123E60);
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(37);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.width.mas_equalTo(K_SCREENWIDTH - 40);
        make.height.mas_equalTo(270);
    }];
    

    UIImageView *logoView = [[UIImageView alloc] initWithImage:k_imageName(@"icon_shellcoin_logo")];
    [self addSubview:logoView];
    [logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(contentView.mas_centerX);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(74);
        make.height.mas_equalTo(74);
    }];
    
    
    ///!!!:时间
    UILabel *timeLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(14) color:UIColorHex(0x627D92)];
    [contentView addSubview:timeLab];
    self.timeLab = timeLab;
    self.timeLab.adjustsFontSizeToFitWidth = YES;
    [timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.mas_equalTo(20);
       make.top.mas_equalTo(54);
       make.width.mas_equalTo(150);
       make.height.mas_equalTo(20);
    }];
        
    ///!!!:剩余数量
    UILabel *remainingNumLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(14) color:UIColorHex(0xFFFFFF)];
    [contentView addSubview:remainingNumLab];
    remainingNumLab.adjustsFontSizeToFitWidth = YES;
    [remainingNumLab lz_rightAlignment];
    [remainingNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(timeLab.mas_centerY);
        make.width.mas_equalTo(K_SCREENWIDTH - 80 - 15 - 150);
        make.height.mas_equalTo(20);
    }];
    
    
    ///!!!:进度条
    UIView *bottomProgressView = [[UIView alloc] init];
    bottomProgressView.cornerRadius = 6;
    bottomProgressView.backgroundColor = UIColorHex(0x28527D);
    [contentView addSubview:bottomProgressView];
    [bottomProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(contentView.mas_centerX);
        make.top.mas_equalTo(84);
        make.width.mas_equalTo(K_SCREENWIDTH - 80);
        make.height.mas_equalTo(12);
    }];
       

    UIView *progressRateView = [[UIView alloc] init];
    progressRateView.cornerRadius = 8;
    progressRateView.backgroundColor = UIColorHex(0x4AB2E7);
    [contentView addSubview:progressRateView];
    [progressRateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(bottomProgressView);
        make.centerY.mas_equalTo(bottomProgressView.mas_centerY);
        make.width.mas_equalTo(K_SCREENWIDTH - 80);
        make.height.mas_equalTo(16);
    }];
       
    
   
    
    
    
    UILabel *progressNumLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(10) color:UIColorHex(0x4AB2E7)];
    [progressRateView addSubview:progressNumLab];
    progressNumLab.backgroundColor = UIColor.whiteColor;
    [progressNumLab lz_centerAlignment];
    progressNumLab.cornerRadius = 7;
    [progressNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bottomProgressView.mas_centerY);
        make.right.mas_equalTo(-1);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(14);
    }];
    

    [progressRateView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo((K_SCREENWIDTH -80) * 0.7);
    }];
    
    ///!!!:价格
    UILabel *priceLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(10) color:UIColorHex(0x627D92)];
    [contentView addSubview:priceLab];
    [priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(remainingNumLab.mas_bottom).mas_offset(35);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(40);
    }];
    
    
    NSString *priceNum = [NSString stringWithFormat:@"%@",self.viewModel.coinModel.price].saveDecimal;
    NSString *priceNumString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"价格(USDT)"),priceNum];
    NSMutableAttributedString *priceNumAttributedString = [NSAttributedString attributedString:priceNumString rangeTitle:priceNum leftFont:k_textFont(10) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
    priceLab.attributedText = priceNumAttributedString;
    
    
    ///!!!:总量
    UILabel *totalLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(10) color:UIColorHex(0x627D92)];
    [totalLab lz_rightAlignment];
    [contentView addSubview:totalLab];
    [totalLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(priceLab.mas_centerY);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(40);
    }];
       
    
    NSString *totalNum = [NSString stringWithFormat:@"%@",self.viewModel.coinModel.total].saveDecimal;
    NSString *totalNumString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"配售池"),totalNum];
    NSMutableAttributedString *totalNumAttributedString = [NSAttributedString attributedString:totalNumString rangeTitle:totalNum leftFont:k_textFont(10) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
    totalLab.attributedText = totalNumAttributedString;
    
    ///!!!:购买
    UIButton *buyBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"预约配售") titleColor:UIColor.whiteColor fontSize:17];
//    UIImage *norImage = [UIImage imageWithColor:UIColorHex(0x64E3AE) size:CGSizeMake(K_SCREENWIDTH - 80, 50)];
//    #8C98B1
    UIImage *norImage = [UIImage gradientImageWithColors:@[UIColorHex(0x22C6FE),UIColorHex(0x65DFFE)] rect:CGRectMake(0, 0, K_SCREENWIDTH - 80, 50)];
    [buyBtn setBackgroundImage:norImage forState:UIControlStateNormal];
    buyBtn.cornerRadius = 10;
    buyBtn.titleLabel.font = k_textBoldFont(17);
    [contentView addSubview:buyBtn];
    [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(totalLab.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(20);
        make.height.mas_equalTo(50);
    }];
    [buyBtn addTarget:self action:@selector(buyClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    ///!!!:已经购买
    UILabel *buyNumLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(14) color:UIColorHex(0x627D92)];
    [contentView addSubview:buyNumLab];
    [buyNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(buyBtn.mas_bottom).mas_offset(12);
        make.width.mas_equalTo(270);
        make.height.mas_equalTo(20);
    }];
    
   

    
    
    
    
    CGFloat totalAmount = [[NSString stringWithFormat:@"%ld",(long)self.viewModel.coinModel.totalAmount] floatValue];
    CGFloat progress = self.viewModel.coinModel.actualAmount / totalAmount;
    if(progress > 1){
        progress = 1;
    }
    CGFloat progressNumWidth = 0;
    CGFloat progressRateViewWidth = 0;
    NSInteger status = [self.viewModel.coinModel.status integerValue];
    switch (status) {
       case 10:
       {
            [buyBtn setTitle:LZLocalizedString(@"即将开始") forState:UIControlStateNormal];
            UIImage *norImage = [UIImage imageWithColor:UIColorHex(0x64E3AE) size:CGSizeMake(K_SCREENWIDTH - 80, 50)];
            [buyBtn setBackgroundImage:norImage forState:UIControlStateNormal];
            buyBtn.userInteractionEnabled = NO;

            ///剩余量
//           NSString *remainingNum = [NSString stringWithFormat:@"%ld",(long)self.viewModel.coinModel.actualAmount];
//           NSString *remainingNumString = [NSString stringWithFormat:@"%ld/%d",(long)self.viewModel.coinModel.actualAmount ,(long)self.viewModel.coinModel.totalAmount];
//            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:remainingNumString];
//            NSDictionary *dic = @{NSFontAttributeName:k_textFont(14),NSForegroundColorAttributeName:UIColorHex(0xFFFFFF)};
//            [attributedString addAttributes:dic range:NSMakeRange(0, remainingNum.length)];
//            NSDictionary *surplusDic = @{NSFontAttributeName:k_textFont(14),NSForegroundColorAttributeName:UIColorHex(0x627D92)};
//            [attributedString addAttributes:surplusDic range:NSMakeRange(remainingNum.length, remainingNumString.length - remainingNum.length)];
//
//            remainingNumLab.attributedText = attributedString;
            progressNumLab.text = LZLocalizedString(@"即将开始");
            progressNumWidth = [progressNumLab.text lz_textWidthWithFontSize:progressNumLab.font withMaxHeight:14] + 7;
            progressRateViewWidth = progressNumWidth + 2;
           
       }
           break;
       case 20:
       {
//            ///剩余量
//            NSString *remainingNum = [NSString stringWithFormat:@"%ld",(long)self.viewModel.coinModel.actualAmount];
//            NSString *remainingNumString = [NSString stringWithFormat:@"%ld/%ld",(long)self.viewModel.coinModel.actualAmount ,(long)self.viewModel.coinModel.totalAmount];
//            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:remainingNumString];
//            NSDictionary *dic = @{NSFontAttributeName:k_textFont(14),NSForegroundColorAttributeName:UIColorHex(0xFFFFFF)};
//            [attributedString addAttributes:dic range:NSMakeRange(0, remainingNum.length)];
//            NSDictionary *surplusDic = @{NSFontAttributeName:k_textFont(14),NSForegroundColorAttributeName:UIColorHex(0x627D92)};
//            [attributedString addAttributes:surplusDic range:NSMakeRange(remainingNum.length, remainingNumString.length - remainingNum.length)];
//           remainingNumLab.attributedText = attributedString;

            progressNumLab.text = [NSString stringWithFormat:@"%.0f%%",progress * 100];
            progressNumWidth = 40;

            CGFloat tempRateViewWidth = (K_SCREENWIDTH - 80) * progress;
            if(tempRateViewWidth < progressNumWidth){
                progressRateViewWidth = progressNumWidth + 2;
            } else {
                progressRateViewWidth = tempRateViewWidth;
            }
       }
           break;
       case 30:
       {
           [buyBtn setTitle:LZLocalizedString(@"已完成") forState:UIControlStateNormal];
           UIImage *norImage = [UIImage imageWithColor:UIColorHex(0x8C98B1) size:CGSizeMake(K_SCREENWIDTH - 80, 50)];
           [buyBtn setBackgroundImage:norImage forState:UIControlStateNormal];
           buyBtn.userInteractionEnabled = NO;
        

            progressNumLab.text = LZLocalizedString(@"100%");
            progressRateView.backgroundColor = UIColorHex(0x64E3AE);
            progressNumWidth = [progressNumLab.text lz_textWidthWithFontSize:progressNumLab.font withMaxHeight:14] + 7;
            progress = 1;
            progressRateViewWidth = K_SCREENWIDTH - 80;
       }
           break;
       default:
           break;
    }
       
    
    remainingNumLab.textColor = UIColorHex(0x627D92);

    NSString *remainingNum = [NSString stringWithFormat:@"%ld",self.viewModel.coinModel.actualAmount];
    NSString *remainingNumString = [NSString stringWithFormat:@"%@ %ld/%ld",LZLocalizedString(@"中签率"),(long)self.viewModel.coinModel.actualAmount ,(long)self.viewModel.coinModel.totalAmount];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:remainingNumString];
    NSDictionary *dic = @{NSFontAttributeName:k_textFont(14),NSForegroundColorAttributeName:UIColorHex(0xFFFFFF)};
    [attributedString addAttributes:dic range:NSMakeRange(LZLocalizedString(@"中签率").length + 1, remainingNum.length)];
    remainingNumLab.attributedText = attributedString;
    
    
    
    [progressNumLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(progressNumWidth);
    }];
    //(K_SCREENWIDTH - 80) * progress
    [progressRateView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(progressRateViewWidth);
    }];
    
    
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        
        [kCountDownManager reload];
        
        
        
        
        if([self.viewModel.coinModel.status isEqualToString:@"30"]){
            
            NSString *buyNum = [[NSString stringWithFormat:@"%@",self.viewModel.coinModel.buyAmount].saveDecimal stringByAppendingFormat:@" USDT"];
                   NSString *buyNumString = [NSString stringWithFormat:@"%@ %@",LZLocalizedString(@"我的配购"),buyNum];
                   NSMutableAttributedString *buyNumAttributedString = [NSAttributedString attributedString:buyNumString rangeTitle:buyNum leftFont:k_textFont(14) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
                   buyNumLab.attributedText = buyNumAttributedString;
        } else {
            NSString *buyNum = [[NSString stringWithFormat:@"%@",self.viewModel.coinModel.buyAmount].saveDecimal stringByAppendingFormat:@" USDT"];
                   NSString *buyNumString = [NSString stringWithFormat:@"%@ %@",LZLocalizedString(@"我的预约"),buyNum];
                   NSMutableAttributedString *buyNumAttributedString = [NSAttributedString attributedString:buyNumString rangeTitle:buyNum leftFont:k_textFont(14) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
                   buyNumLab.attributedText = buyNumAttributedString;
            
        }
        
        
        
        
        
       
    }];
}

//购买
- (void)buyClick:(UIButton *)sender{
    
    NSString *limit = [NSString stringWithFormat:@"%@",self.viewModel.coinModel.limit].saveDecimal;
    NSString *placeholder = [NSString stringWithFormat:LZLocalizedString(@"不小于%@USDT"),limit];
    @weakify(self);
    [LZToolView showAlertType:LZAlertViewTypeSubscribe withTitle:LZLocalizedString(@"认购") message:LZLocalizedString(@"划入USDT数量") cancel:LZLocalizedString(@"取消") sure:LZLocalizedString(@"确定") objectDict:@{@"placeholder":placeholder} cancelBlock:^(id  _Nullable object) {
    } sureBlock:^(id  _Nullable object) {
        @strongify(self);
        NSDictionary *dataDictionary = object;
        [self.viewModel crowdBuy:dataDictionary[@"number"] payPassword:dataDictionary[@"password"]];
        
    }];
    
    
}

#pragma mark - 倒计时通知回调
- (void)countDownNotification {
    
    /// 计算倒计时
    NSInteger countDown = [self.viewModel.coinModel.remainingSecond integerValue] - kCountDownManager.timeInterval;//self.orderModel.remainingSeconds - kCountDownManager.timeInterval;
    if (countDown < 0) {
      countDown = 0;
    };
   NSString *time = [NSString stringWithFormat:@"%02zd:%02zd:%02zd",(countDown/3600)%60, (countDown/60)%60, countDown%60];

   NSInteger status = [self.viewModel.coinModel.status integerValue];
   NSString *timeStatue = @"";
      switch (status) {
          case 10:
          {
               timeStatue = LZLocalizedString(@"距离开始");
               NSString *timeString = [NSString stringWithFormat:@"%@ %@",timeStatue,time];
               NSMutableAttributedString *timeAttributedString = [NSAttributedString attributedString:timeString rangeTitle:time leftFont:k_textFont(14) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
               self.timeLab.attributedText = timeAttributedString;
                 
          }
              break;
          case 20:
           {
               timeStatue = LZLocalizedString(@"距离结束");
               NSString *timeString = [NSString stringWithFormat:@"%@ %@",timeStatue,time];
               NSMutableAttributedString *timeAttributedString = [NSAttributedString attributedString:timeString rangeTitle:time leftFont:k_textFont(14) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
               self.timeLab.attributedText = timeAttributedString;


           }
               break;
          case 30:
           {
               NSString *timeString = [NSString stringWithFormat:@"%@ %@",LZLocalizedString(@"完成时间"),self.viewModel.coinModel.completeTime];
               NSMutableAttributedString *timeAttributedString = [NSAttributedString attributedString:timeString rangeTitle:self.viewModel.coinModel.completeTime leftFont:k_textFont(14) rightFont:k_textFont(14) leftColor:UIColorHex(0x627D92) rightColor:UIColorHex(0xFFFFFF)];
               self.timeLab.attributedText = timeAttributedString;
           }
               break;
          default:
              
              break;
      }
}

@end
